$('#sax-adunits').click(function(event){
   $('#section0').empty();
   $('#section1').empty();
   $('#section2').empty();
    var id = "sax-adunits-table";
    var boxid = "box-" + id;
    if(boxExist(boxid)){boxDisplay(boxid);}else{
        addBox("section0", id, "SAX广告位列表", 0, "", "", "app");
        $.ajax({
            url: "/sax/adunits",
            dataType: "json",
            timeout: 20000
        })
        .done(function(data){
            console.log(data)
            $("#table-" + id).DataTable({
                data: data.values,
                "scrollX": true,
                columns: data.colums,
                "initComplete": function(){
                    var api = this.api();
                    api.$('td').click(function(){
                        api.search(this.innerHTML).draw();
                        var tr = $(this).parent('tr').first();
                        console.log(tr);
                        var tdhead = tr.children('td')[0];
                        var adunitid = tdhead.innerHTML;
                        var adunitname = tr.children('td')[1].innerHTML;

                        //添加扶翼的实时投放数据
                        var adunitBoxId = adunitid + "-rt";

                        $('#box-sub-' + id).empty();
                        $('#box-sub-' + id).append($('<div class="row" id="bts-adunits"></div>'));

                        //添加广告位线上配置信息 模态窗口显示
                        var adunitinfomodal = '<!-- Button trigger modal -->' +
                          '<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#'+adunitid+'-Modal">' +
                            '管理端配置' +
                          '</button>' +
                          '<!-- Modal -->' +
                          '<div class="modal fade" id="'+adunitid+'-Modal" tabindex="-1" role="dialog" aria-labelledby="'+ adunitid +'-ModalLabel">' +
                            '<div class="modal-dialog" role="document">' +
                              '<div class="modal-content">' +
                                '<div class="modal-header">' +
                                  '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
                                  '<h4 class="modal-title" id="'+ adunitid +'-ModalLabel">'+ adunitname +'</h4>' +
                                '</div>' +
                                '<div class="modal-body">' +

                                '</div>' +
                              '</div>' +
                            '</div>' +
                          '</div>'

                        $('#bts-adunits').append($(adunitinfomodal));
                        $('#bts-adunits').append($('<div class="col-lg-6">'+
                            '<button type="button" class="btn btn-default pull-right" id="adunit-rt-date-picker">' +
                                '<span>' +
                                    '<i class="fa fa-calendar"></i> Date range picker' +
                                '</span>' +
                                    '<i class="fa fa-caret-down"></i>' +
                            '</button>' +
                        '</div>'));

                        $('#' + adunitid + '-Modal').on('show.bs.modal', function (event) {
                          //var button = $(event.relatedTarget) // Button that triggered the modal
                          //var recipient = button.data('whatever') // Extract info from data-* attributes
                          // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
                          // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
                          var modal = $(this)
                          //modal.find('.modal-title').text('New message to ' + recipient)
                          modal.find('.modal-body').html("<p>查询数据。。</p>");
                          $.ajax({
                            url: "http://10.13.88.161:9000/adunitconfiginfo",
                            data: {id: adunitid},
                            timeout: 20000,
                            dataType: "text"
                          })
                          .done(function(data){
                            console.log(data);
                            modal.find('.modal-body').html(data);
                          })
                          .fail(function( xhr, status, errorThrown ) {
                               alert( "获取广告位配置信息失败" );
                               console.log( "Error: " + errorThrown );
                               console.log( "Status: " + status );
                               console.dir( xhr );
                             })
                        })


                        //添加时间控件
                        function cbqps(start, end) {
                             $('#adunit-rt-date-picker' + ' span').html(start.format('YYYY-MM-DD HH:mm') + ' - ' + end.format('YYYY-MM-DD HH:mm'));
                        }

                        var start = moment().startOf("day");
                        var end = moment();
                        $('#adunit-rt-date-picker').daterangepicker(
                           {
                             ranges: {
                               '过去15m': [moment().subtract(15, "m"), moment()],
                               '过去1h': [moment().subtract(1, 'h'), moment()],
                               '过去2h': [moment().subtract(2, 'h'), moment()],
                               '过去3h': [moment().subtract(3, 'h'), moment()],
                               '过去4h': [moment().subtract(4, 'h'), moment()],
                               '过去6h': [moment().subtract(6, 'h'), moment()],
                               '今天': [moment().startOf("day"), moment()],
                               '过去1天': [moment().subtract(1, 'days'), moment()],
                               '过去2天': [moment().subtract(2, 'days'), moment()],
                               '过去3天': [moment().subtract(3, 'days'), moment()],
                               '过去4天': [moment().subtract(4, 'days'), moment()]
                             },
                             startDate: start,
                             endDate: end,
                             timePicker: true
                           }, cbqps);

                        //添加广告位相关数据实时统计

                        $('#box-sub-' + id).append($('<div class="row" id="audniti-rta-charts"></div>'));

                        //添加 扶翼数据统计
                        $('#audniti-rta-charts').append($('<div class="col-lg-4" >' +
                               '<div id="fydspchart"  style="height:300px;"></div>' +
                           '</div>'))
                        $('#audniti-rta-charts').append($('<div class="col-lg-4" >' +
                               '<div id="enginechart"  style="height:300px;"></div>' +
                           '</div>'))
                        $('#audniti-rta-charts').append($('<div class="col-lg-4" >' +
                               '<div id="produclinechart"  style="height:300px;"></div>' +
                           '</div>'))
                        var fychart = createChart("fydspchart", "扶翼投放数据", "你好", "new");
                        var enginechart = createChart("enginechart", "引擎层级投放数据", "pv", "new")
                        var produclinechart = createChart("produclinechart", "展示产品线数据", "pv", "new")


                        $('#adunit-rt-date-picker').on('apply.daterangepicker', function(ev, picker) {


                          var start = picker.startDate.valueOf()
                          var end = picker.endDate.valueOf()
                          $.ajax({
                              url: "/dsp/deliveryinfo/fy",
                              dataType: "json",
                              data: {
                                  adunitid: adunitid,
                                  inter: "15m",
                                  start: start,
                                  end: end,
                              },
                              timeout: 20000,
                          })
                          .done(function(json){
                              console.log(json[0]);
                              for (var i = fychart.series.length -1; i >= 0; i --){
                                fychart.series[i].remove();
                              }
                              fychart.addSeries(json[0]);
                              fychart.addSeries(json[1]);
                              fychart.addSeries(json[2]);
                              fychart.addSeries(json[3]);
                              fychart.addSeries(json[4]);
                              console.log(fychart.options);
                          });
                          $.ajax({
                              url: "/engine/deliveryinfo",
                              dataType: "json",
                              data: {
                                  adunitid: adunitid,
                                  inter: "1m",
                                  start: start,
                                  end: end,
                              },
                              timeout: 20000,
                          })
                          .done(function(json){
                              console.log(json[0]);
                              for (var i = enginechart.series.length -1; i >= 0; i --){
                                enginechart.series[i].remove();
                              }
                              enginechart.addSeries(json[0]);
                              enginechart.addSeries(json[1]);
                              enginechart.addSeries(json[2]);
                              enginechart.addSeries(json[3]);
                              enginechart.addSeries(json[4]);
                              enginechart.addSeries(json[5]);
                              console.log(fychart.options);
                          });

                          $.ajax({
                              url: "/brand/adunitproductline",
                              dataType: "json",
                              data: {
                                  adunitid: adunitid,
                                  inter: "1m",
                                  start: start,
                                  end: end,
                              },
                              timeout: 20000,
                          })
                          .done(function(json){
                              console.log(json[0]);
                              for (var i = produclinechart.series.length -1; i >= 0; i --){
                                produclinechart.series[i].remove();
                              }
                              produclinechart.addSeries(json[0]);
                              produclinechart.addSeries(json[1]);
                              produclinechart.addSeries(json[2]);
                              produclinechart.addSeries(json[3]);
                              produclinechart.addSeries(json[4]);
                              produclinechart.addSeries(json[5]);
                              produclinechart.addSeries(json[6]);
                              console.log(fychart.options);
                          });

                        });


                        $('#adunit-rt-date-picker').trigger('apply.daterangepicker', $('#adunit-rt-date-picker').data('daterangepicker'));

                    }) //end click
                }
            });

        })
        .fail(function(xhr, status, errorThrown){
           alert("获取SAX的所有广告位信息错误 " + status);
           console.log("Error: " + errorThrown);
           console.log("Status: " + status);
           $("#table-" + id).remove();
        });



    }
})
